<?php
/**
 * Created by PhpStorm.
 * User: ghost
 * Date: 2016/7/28
 * Time: 13:28
 */

namespace Home\Model;


class VerdictModel extends Model
{
    public function import($file)
    {
        $count = 0;
        $fields = ['month', 'city', 'site_name', 'project_code', 'unit', 'profession', 'send_money', 'verdict_money', 'contract_money', 'sub_money', 'sub_rate'];

        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = \PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

        for ($row = 2; $row <= $highestRow; ++$row) {
            $rowData = [];
            for ($col = 0; $col < $highestColumn; $col++) {
                $value = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $row)->getValue();
                $rowData[] = is_null($value) ? '' : $value;
            }

            if ($rowData) {
                array_shift($rowData);
                if ($this->add(array_combine($fields, $rowData))) {
                    ++$count;
                }
            }
        }

        return $count;
    }

    public function getSomeBatchs()
    {
        return $this->group('month')->order('id desc')->limit(0, 60)->getField('month', true);
    }
}
